package com.xyzq.cgate.core.handler;

import com.google.common.collect.Maps;
import com.xyzq.cgate.common.response.ResponseEntity;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.ResourceProperties;
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
import org.springframework.boot.web.reactive.error.ErrorAttributes;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.server.ServerRequest;

import java.util.Map;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/19 14:21
 */
@Deprecated  //使用GlobalErrorWebExceptionHandler
public class GlobalErrorHandler extends DefaultErrorWebExceptionHandler {

    public GlobalErrorHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
                              ErrorProperties errorProperties, ApplicationContext applicationContext) {
        super(errorAttributes, resourceProperties, errorProperties, applicationContext);
    }

    @Override
    protected HttpStatus getHttpStatus(final Map<String, Object> errorAttributes) {
        return HttpStatus.INTERNAL_SERVER_ERROR;
    }

    @Override
    protected Map<String, Object> getErrorAttributes(final ServerRequest request, final boolean includeStackTrace) {
        Throwable error = super.getError(request);
        error.printStackTrace();
        return responseMap();
    }

    private Map<String,Object> responseMap() {
        Map<String, Object> map = Maps.newHashMapWithExpectedSize(3);
        map.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
        map.put("msg", HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase());
        map.put("data", null);
        return map;
    }
}
